package fiuba.model;

import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.userdetails.UserDetails;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;


import javax.persistence.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Entity
public class Menu extends BaseObject {

	private static final long serialVersionUID = 3842626162173359411L;

    private Long id;
    private Set<MenuItem> menuItems = new HashSet<MenuItem>();
    private String name;
    
    private String colorItem;
    private String colorItemHover;
    private String colorFondoItem;
    private String colorFondoItemHover;
    private String fuenteItem;
    private String tamanioFuenteItem;
    private String tamanioFuenteItemHover;
    private String altoMenuItem;
    private String orden;
    
    @Column(length=100)
    public String getOrden() {
		return orden;
	}
	public void setOrden(String orden) {
		this.orden = orden;
	}
	//constructor 
    public Menu (final String name){
    	this.name = name;
    }
    /**
     * Default constructor - creates a new instance with no values set.
     */
    public Menu() {}

    
    @ManyToMany(fetch = FetchType.EAGER) 
    @JoinTable(
            name="menu_menuItem",
            joinColumns = { @JoinColumn( name="menu_id") },
            inverseJoinColumns = @JoinColumn( name="menuItem_id")
    )    
	public Set<MenuItem> getMenuItems() {
    	
		return menuItems;
	}
    
    @Transient
    public void addMenuItem(MenuItem menuItem){
    	getMenuItems().add(menuItem);
    }
    
	public void setMenuItems(Set<MenuItem> menuItems) {
		this.menuItems = menuItems;
	}
	@Id @GeneratedValue(strategy=GenerationType.AUTO)
	public Long getId() {
		return id;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = prime * ((id == null) ? 0 : id.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (getClass() != obj.getClass())
			return false;
		final Menu other = (Menu) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();        
        if (this.menuItems != null) {
            sb.append("Menu Items: ");
            sb.append(this.menuItems.toString());
            /*for (int i = 0; i < this.menuItems.size(); i++) {
                if (i > 0) {
                    sb.append(", ");
                }
                sb.append(this.menuItems.toString());
            }*/
        } else {
            sb.append("No Granted Authorities");
        }
        return sb.toString();
    }
	
	@Column(nullable=false,length=50,unique=true)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	/**
     * Convert user roles to LabelValue objects for convenience.
     * @return a list of LabelValue objects with role information
     */
    @Transient
    public List<LabelValue> getItemList() {
        List<LabelValue> menuItems = new ArrayList<LabelValue>();

        if (this.menuItems != null) {
            for (MenuItem item : this.menuItems) {
                // convert the user's roles to LabelValue Objects
                menuItems.add(new LabelValue(item.getName(),item.getId().toString()));
            }
        }

        return menuItems;
    }
    
    @Column(length=5)
	public String getTamanioFuenteItemHover() {
		return tamanioFuenteItemHover;
	}
	public void setTamanioFuenteItemHover(String tamanioFuenteItemHover) {
		this.tamanioFuenteItemHover = tamanioFuenteItemHover;
	}
	@Column(length=10)
	public String getColorItem() {
		return colorItem;
	}
	public void setColorItem(String colorItem) {
		this.colorItem = colorItem;
	}
	@Column(length=10)
	public String getColorItemHover() {
		return colorItemHover;
	}
	public void setColorItemHover(String colorItemHover) {
		this.colorItemHover = colorItemHover;
	}
	@Column(length=10)
	public String getColorFondoItem() {
		return colorFondoItem;
	}
	public void setColorFondoItem(String colorFondoItem) {
		this.colorFondoItem = colorFondoItem;
	}
	@Column(length=10)
	public String getColorFondoItemHover() {
		return colorFondoItemHover;
	}
	public void setColorFondoItemHover(String colorFondoItemHover) {
		this.colorFondoItemHover = colorFondoItemHover;
	}
	@Column(length=50)
	public String getFuenteItem() {
		return fuenteItem;
	}
	public void setFuenteItem(String fuenteItem) {
		this.fuenteItem = fuenteItem;
	}
	@Column(length=5)
	public String getTamanioFuenteItem() {
		return tamanioFuenteItem;
	}
	public void setTamanioFuenteItem(String tamanioFuenteItem) {
		this.tamanioFuenteItem = tamanioFuenteItem;
	}
	@Column(length=5)
	public String getAltoMenuItem() {
		return altoMenuItem;
	}
	public void setAltoMenuItem(String altoMenuItem) {
		this.altoMenuItem = altoMenuItem;
	}

}
